#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/10 9:14
# @Author  : Devin
# @Site    : 
# @File    : DataProcessing.py
# @Software: PyCharm Edu
import pandas
from cassandra.cluster import Cluster
from cassandra import *
from cassandra.auth import PlainTextAuthProvider
from cassandra.query import SimpleStatement
import logging
import configparser
import sys
cf = configparser.ConfigParser()
cf.read("conf/train.conf")

def get_name_by_program(df1, class_index):  # 獲取程序名列表，并創建新DataFrame，增加了“class”這一列
    df = df1.copy()
    if len(class_index) == 0:
        df["class"] = "default"
        return df , set(["default"])
    def create_program_name(df):
        class_list = []
        for index in list(df.index):
            class_name = ""
            for column in class_index:
                class_name += str(df[column][index]) + "_"
            class_list.append(class_name.rsplit("_", 1)[0])
        df["class"] = class_list

    create_program_name(df)
    return df, set(df["class"])


def split_df(df2, class_list):  # 根據class將大的DataFrame分解為幾個小的DataFrame，返回一個dict
    df = df2.copy()  # dict類似于{class_name1: DataFrame1, class_name2: DataFrame2，...}
    getter = {}
    df_dict = {}
    for name in class_list:
        df_dict[name] = []
        getter[name] = []
    for name in class_list:
        for index in df.index:
            if df["class"][index] == name:
                getter[name].append(index)

        df_dict[name] = pandas.DataFrame(df,index = getter[name])
    return df_dict

def get_df_from_cassandra(address):
    ap = PlainTextAuthProvider(username=address["user"],password=address["password"])
    cassandra_ip = address["host"]
    cluster = Cluster([cassandra_ip], auth_provider=ap)

    session = cluster.connect()

    def pandas_factory(colnames, rows):
        return pandas.DataFrame(rows, columns=colnames)

    session.row_factory = pandas_factory
    session.default_fetch_size = cf.getint("number", "fetch_size")
    try:
        rslt = session.execute(SimpleStatement("select * from {}.{}   ;".format(address["keyspace"],address["table"]),consistency_level=ConsistencyLevel.ONE), timeout=None)
        #rslt = session.execute(SimpleStatement("select * from {}.{} where id='gw_id_e8578676088f11e8a937005056b6166b'  ;".format(address["keyspace"],address["table"]),consistency_level=ConsistencyLevel.ONE), timeout=None)
    except Exception as e:
        logging.error(str(e))
        rslt = "error"
    return rslt, cluster
    #return rslt._current_rows
